package com.tql.mall.boss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tql.mall.boss.mapper.BaseAttrValueMapper;
import com.tql.mall.boss.service.BaseAttrValueService;
import com.tql.mall.model.enums.category.OperFailEnum;
import com.tql.mall.model.product.BaseAttrValue;
import com.tql.mall.model.product.BaseAttrValueQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class BaseAttrValueServiceImpl extends ServiceImpl<BaseAttrValueMapper, BaseAttrValue> implements BaseAttrValueService {

    @Override
    public List<BaseAttrValue> getAttrValueListByAttrId(Long id) {
        QueryWrapper<BaseAttrValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attr_id",id);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<BaseAttrValue> getAttrValueList(BaseAttrValueQueryVo baseAttrValueQueryVo) {
        QueryWrapper<BaseAttrValue> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(baseAttrValueQueryVo.getAttrId())){
            queryWrapper.eq("attr_id",baseAttrValueQueryVo.getAttrId());
        }
        if (!StringUtils.isEmpty(baseAttrValueQueryVo.getValueName())){
            queryWrapper.like("value_name",baseAttrValueQueryVo.getValueName());
        }
        if(!StringUtils.isEmpty(baseAttrValueQueryVo.getId())){
            queryWrapper.eq("id",baseAttrValueQueryVo.getId());
        }
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public String updateAttrValue(Long id, String newName) {
        //先根据id查询 分类
        BaseAttrValue baseAttrValue = baseMapper.selectById(id);
        //名字相同，则不执行修改
        if (baseAttrValue.getValueName().equals(newName)){
            return null;
        }
        //查询是否有和newName重名的属性值
        QueryWrapper<BaseAttrValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("value_name",newName);
        BaseAttrValue baseAttrValue1 = baseMapper.selectOne(queryWrapper);
        if (baseAttrValue1 != null){//重名了
            return OperFailEnum.EDIT_FILE_NAME_DEPLICATED.getMsg();
        }
        //执行修改
        baseAttrValue.setValueName(newName);
        baseAttrValue.setUpdateTime(new Date());
        baseMapper.updateById(baseAttrValue);
        return null;
    }

    @Override
    public String addAttrValue(Long attrId, String name) {
        //查询一级分类中是否有和newName重名的分类
        QueryWrapper<BaseAttrValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("value_name",name);
        queryWrapper.eq("attr_id",attrId);
        BaseAttrValue baseAttrValue1 = baseMapper.selectOne(queryWrapper);
        if (baseAttrValue1 != null){//重名了
            return OperFailEnum.EDIT_FILE_NAME_DEPLICATED.getMsg();
        }
        //执行添加
        BaseAttrValue baseAttrValue = new BaseAttrValue();
        baseAttrValue.setValueName(name);
        baseAttrValue.setAttrId(attrId);
        baseMapper.insert(baseAttrValue);
        return null;
    }
}
